//
//  UIImage+Zoom.m
//  DealWithImage
//
//  Created by 李明明 on 15/9/29.
//  Copyright © 2015年 李明明. All rights reserved.
//

#import "UIImage+Zoom.h"

@implementation UIImage (Zoom)
- (UIImage *)cropImageWithRect:(CGRect)rect
{
    CGImageRef imageRef = self.CGImage;
    rect = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width * self.scale, rect.size.height * self.scale);
    imageRef = CGImageCreateWithImageInRect(imageRef, rect);
       UIImage *image = [UIImage imageWithCGImage:imageRef];
    image = [image makeZoomImageWithSacle:1];
    CGImageRelease(imageRef);
    return image;
}
- (UIImage *)makeZoomImageWithSacle:(CGFloat)scale
{
    // 设置放大缩小的大小
    CGSize size = CGSizeMake(self.size.width * scale * self.scale, self.size.width * scale * self.scale);
    CGRect rect = CGRectMake(0, 0, size.width, size.height);
    // 设置画布
    UIGraphicsBeginImageContext(size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGAffineTransform flip = CGAffineTransformMakeScale(1, -1);
    CGAffineTransform flipThenShift = CGAffineTransformTranslate(flip, 0, size.height);
    CGContextConcatCTM(context, flipThenShift);
    CGContextDrawImage(context, rect, self.CGImage);
    CGContextSetBlendMode(context, kCGBlendModeNormal);
    CGRect transformedRect = CGRectApplyAffineTransform(rect, flipThenShift);
    CGContextDrawImage(context, transformedRect, self.CGImage);
    UIImage *imageAfterChange = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    /* // 变黑白
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
    context = CGBitmapContextCreate(nil, size.width, size.height, 8.0, 0, colorSpace, (CGBitmapInfo)kCGImageAlphaNone);
    CGContextDrawImage(context, rect, imageAfterChange.CGImage);
    CGImageRef imageRef = CGBitmapContextCreateImage(context);
    UIImage *finalImage = [UIImage imageWithCGImage:imageRef];
//    CGAffineTransformMakeRotation(1.57);
    CGColorSpaceRelease(colorSpace);
    CGContextRelease(context);
    CFRelease(imageRef);
    */
    return imageAfterChange;
}
- (UIImage *)filterWithName:(NSString *)filterName
{
    UIImage *img;
    CIImage *beginImage = [CIImage imageWithCGImage:self.CGImage];
    CIFilter *filter = [CIFilter filterWithName:filterName];
    [filter setValue:beginImage forKey:kCIInputImageKey];
    [filter setValue:[NSNumber numberWithFloat:1.1] forKey:@"inputSaturation"];
    [filter setValue:[NSNumber numberWithFloat:0] forKey:@"inputBrightness"];
    [filter setValue:[NSNumber numberWithFloat:1.1] forKey:@"inputContrast"];
    CIImage *outputImage = [filter outputImage];
    CIContext *context = [CIContext contextWithOptions:nil];
    CGImageRef cgimage = [context createCGImage:outputImage fromRect:[outputImage extent]];
    img = [UIImage imageWithCGImage:cgimage];
    CGImageRelease(cgimage);
    return img;
}
- (UIImage *)imageRotate:(CGFloat)rotate
{
    CGSize imageSize = CGSizeMake(self.size.width * self.scale, self.size.height * self.scale);
    CGRect rect = CGRectMake(0, 0, imageSize.width, imageSize.height);
    rect = CGRectApplyAffineTransform(rect, CGAffineTransformMakeRotation(rotate));
    imageSize = CGSizeMake(CGRectGetWidth(rect), CGRectGetHeight(rect));
    UIGraphicsBeginImageContext(imageSize);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(context, imageSize.width / 2, imageSize.height / 2);
    CGContextRotateCTM(context, rotate);
    CGContextTranslateCTM(context, - imageSize.width / 2, - imageSize.height / 2);
    [self drawInRect:CGRectMake(0, 0, imageSize.width , imageSize.height)];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}









@end
